/*** MOBILITY REGRESSIONS BY WEALTH AND INCOME ***/

set more 1
clear all

do "paths.do"

capture log close
log using "$LOGFILES\track_mob_6.log", replace

/*
Retrieve income and wealth measures from public data, txtr
*/
use "$TXTRDATA/txtr_4a.dta", clear
keep if grade == 4
** dfvtott0 and denrtot0 are in logs
gen wealth_per_pupil = exp(dfvtott0 - denrtot0)
keep campus year wealth_per_pupil cgdisadv
rename (campus year) (base_campus base_year)
save "$WORKING/temp/track_mob_6-wealth.dta", replace

/*
Immediately pre-regression mobility sample
*/
use "$WORKING/track_mob_E.dta", clear

merge m:1 base_campus base_year using "$WORKING/temp/track_mob_6-wealth.dta"
keep if _merge == 3
drop _merge

egen tmp1 = sum(wealth_per_pupil * obs), by(base_district)
egen tmp2 = sum(cgdisadv * obs), by(base_district)
egen tmp3 = sum(obs), by(base_district)
gen dist_wealth = tmp1 / tmp3
gen dist_lowinc = tmp2 / tmp3
drop tmp*

sum dist_wealth [w=obs], detail
gen tmp1 = r(p50)
gen dist_wealth_hilo = dist_wealth >= tmp1

sum dist_lowinc [w=obs], detail
gen tmp2 = r(p50)
gen dist_lowinc_hilo = dist_lowinc >= tmp2
drop tmp*

save "$WORKING/temp/track_mob_6-ready.dta", replace


/*
Regress test score mobility measures on tracking exposure
*/

global CTRLVARS1 cy_score_mn cy_score_sd
global CTRLVARS2 has_year1 has_year2 has_year3 has_year4
global TREAT_VARS trk_elem_x trk_mid_x
global INSTR_VARS trk_elem_z trk_mid_z
global CTRLFLEX cy_score_p10 cy_score_p25 cy_score_p75 cy_score_p90

global TREAT_VARS2 trk_elem_x trk_mid_x trk_elem_x2 trk_mid_x2
global INSTR_VARS2 trk_elem_z trk_mid_z trk_elem_z2 trk_mid_z2

global OUT1 scoreresid1 scoreresid2 scoreresid3 scoreresid4

** loop over years after grade 3
foreach yy in $OUT1 {
	disp "`yy'"
	global CTRLVARS $CTRLVARS1 $CTRLVARS2
	
	** loop over wealth/income measures
	foreach sv in wealth lowinc {
		
		** loop over top/bottom half of wealth/income measure
		foreach svv in 0 1 {	
			
			** loop over absolute/relative tracking measures
			foreach xx in unadj rel {
				capture drop trk_elem_x trk_mid_x
				gen trk_elem_x = trk_`xx'_elem_cy_dm
				gen trk_mid_x = trk_`xx'_mid_cy_dm
				
				capture drop samp2
				gen samp2 = samp2_`xx'
				
				** loop over relevant samples
				foreach ss in samp0 samp1 {
					foreach pp in 25 75 50 {
						
						** ols
						reghdfe pred_`pp'_`yy'_half $CTRLVARS $CTRLFLEX $TREAT_VARS if ((`ss') & (dist_`sv'_hilo == `svv')) [aw=obs], absorb(base_year base_campus) vce(cluster base_district)
						eststo, title("mob_`pp'_`yy'::trk_`xx'_cy_dm::yc::`sv'`svv'::`ss'")
						qui sum pred_`pp'_`yy'_half if e(sample)
						estadd scalar mn_out = r(mean)
						
						foreach yg in bm am {
							foreach yygg in cy dy {
								di "mob_`pp'_`yy'::trk_`xx'_`yygg'_`yg'::yc::`sv'`svv'::`ss'"
								
								capture drop trk_elem_z trk_mid_z
								
								gen trk_elem_z = trk_`xx'_elem_`yygg'_`yg'
								gen trk_mid_z = trk_`xx'_mid_`yygg'_`yg'
								
								** instrumental variables
								ivreghdfe pred_`pp'_`yy'_half $CTRLVARS $CTRLFLEX ( $TREAT_VARS = $INSTR_VARS ) ///
									if ((`ss') & (dist_`sv'_hilo == `svv')) [aw=obs], absorb(base_year base_campus) vce(cluster base_district)
								eststo, title("mob_`pp'_`yy'::trk_`xx'_`yygg'_`yg'::yc::`sv'`svv'::`ss'")
								qui sum pred_`pp'_`yy'_half if e(sample)
								estadd scalar mn_out = r(mean)
							}
						}
					}
				}
			}
		}
	}	
	esttab using "$WORKING/track_mob_6-`yy'.csv", csv b(%9.5f) se(%9.5f) r2 ar2 ///
			scalars(mn_out N_clust) sfmt(%9.8g) nostar wide mlabels(,titles) replace
	estimates clear
}

